#!/usr/bin/env node
const path = require('path');
const { program } = require('commander');
const { execSync } = require('child_process');
const { exit } = require('process');

const staticPath = path.resolve(path.join(__dirname, '../static'));
const templatePath = path.resolve(path.join(__dirname, '../template'));

console.log(__dirname, staticPath, templatePath);

program
  .option('-m, --model <model-path>', '模型目录')
  .option('-o, --output <output-path>', '输出目录, 默认输出: 模板目录/out')
  .option('--app [app-code-name]', '发布模式改为应用, 模型目录将识别为应用')
  .option(
    '-tv, --template-version <template-version>',
    '使用发布的模版包的版本号',
    'latest',
  )
  .parse(process.argv);

const { model, output, app, templateVersion } = program.opts();

console.log(program.opts());

// *安装@ibiz-template/vue3-full-template
try {
  execSync('npm i -g @ibiz-template/vue3-full-template@' + templateVersion);
  console.log(`安装@ibiz-template/vue3-full-template的${templateVersion}版本成功`);
} catch (error) {
  console.error('安装@ibiz-template/vue3-full-template包错误：',error);
  exit(1)
}

try {
  execSync(`ibz-full-template -m ${model} -o ${output} --app ${app}`);
  console.log(`发布成功`);
} catch (error) {
  console.error('发布失败：',error);
  exit(1)
}
